package com.easygoods.service;

import com.bestvike.stone.core.lang.StringUtils;
import com.bestvike.stone.spring.exception.BusinessException;
import com.bestvike.stone.spring.exception.ErrorCode;
import com.bestvike.stone.spring.service.BaseService;
import com.easygoods.dao.MemberInfoDao;
import com.easygoods.entity.MemberInfo;
import com.easygoods.enums.YNEnum;
import com.easygoods.request.ChangePayPwdByOldRequest;
import com.easygoods.request.ChangePayPwdBySmsRequest;
import com.easygoods.response.HasPayPwdResponse;
import com.easygoods.shiro.realm.UserRealm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by 许崇雷 on 2019-04-01.
 */
@Service
public class PayPwdService extends BaseService {

    @Autowired
    private MemberService memberService;
    @Autowired
    private SmsService smsService;
    @Autowired
    private MemberInfoDao memberInfoDao;

    //是否设置了支付密码
    public HasPayPwdResponse hasPayPwd() {
        final MemberInfo memberInfo = this.memberService.getMemberInfo();

        final HasPayPwdResponse response = new HasPayPwdResponse();
        response.setHasPayPwd(YNEnum.parse(StringUtils.isNotEmpty(memberInfo.getPayPwd())));
        response.setRemainCash(memberInfo.getRemainCash());
        return response;
    }

    //根据验证码修改支付密码
    public void changePayPwdBySms(ChangePayPwdBySmsRequest request) {
        this.smsService.checkChangePayPwdSms(request);//验证短信
        final MemberInfo memberInfo = this.memberService.getMemberInfo();
        this.updatePayPwdCore(memberInfo, request.getNewPayPwd());
    }

    //根据旧密码修改支付密码
    public void changePayPwdByOld(ChangePayPwdByOldRequest request) {
        final MemberInfo memberInfo = this.memberService.getMemberInfo();
        final String encryptPayPwdOld = UserRealm.encryptPassword(memberInfo.getId(), request.getOldPayPwd(), memberInfo.getPwdSalt());
        if (!StringUtils.equals(encryptPayPwdOld, memberInfo.getPayPwd()))
            throw new BusinessException(ErrorCode.ERROR_SERVER, "原支付密码错误");
        this.updatePayPwdCore(memberInfo, request.getNewPayPwd());
    }

    //更改支付密码
    private void updatePayPwdCore(MemberInfo memberInfo, String newPayPwd) {
        final String encryptPayPwd = UserRealm.encryptPassword(memberInfo.getId(), newPayPwd, memberInfo.getPwdSalt());
        final MemberInfo memberInfoUpdate = new MemberInfo();
        memberInfoUpdate.setId(memberInfo.getId());
        memberInfoUpdate.setPayPwd(encryptPayPwd);
        final int count = this.memberInfoDao.updateByPrimaryKeySelective(memberInfoUpdate);
        if (count != 1)
            throw new BusinessException(ErrorCode.ERROR_SERVER, "修改支付密码失败");
    }

    //验证支付密码是否正确
    public void checkPayPwd(String payPwd) {
        final MemberInfo memberInfo = this.memberService.getMemberInfo();
        final String encryptPayPwd = UserRealm.encryptPassword(memberInfo.getId(), payPwd, memberInfo.getPwdSalt());
        if (!StringUtils.equals(encryptPayPwd, memberInfo.getPayPwd()))
            throw new BusinessException(ErrorCode.ERROR_SERVER, "支付密码错误");
    }
}
